package com.ruoyi.produce.process.scheduling.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.produce.base.bom.domain.ProductionMachine;
import com.ruoyi.produce.process.scheduling.domain.Scheduling;
import com.ruoyi.produce.process.scheduling.service.ISchedulingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/produce/scheduling")
@Api(value = "排产模块", tags = "排产接口")
public class SchedulingController extends BaseController {
    @Autowired
    private ISchedulingService schedulingService;
    /**
     * 获取排产记录
     * @param scheduling
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("获取排产记录")
    public TableDataInfo list(@RequestBody Scheduling scheduling){

        startPage();
        List<Scheduling> schedulings = schedulingService.selectSchedulingList(scheduling);

        return getDataTable(schedulings);
    }
    /**
     * 新增排产记录
     * @param scheduling
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增排产记录")
    public AjaxResult add(@RequestBody Scheduling scheduling){
        Scheduling scheduling1 = schedulingService.searchSchedulingByWorkSheetId(scheduling.getShiftInfoId(), scheduling.getWorkSheetId());
        if (StringUtils.isNotNull(scheduling1)) {
            // 不可以排
            return AjaxResult.error("当前班次:" + scheduling.getShiftInfoId() + "的加工单：" + scheduling.getWorkSheetId() + "已经下达排查，不可以重复排产");
        }
        return toAjax(schedulingService.insertScheduling(scheduling));
    }

    /**
     * 修改排产状态
     * @param scheduling
     * @return
     */
    @PostMapping("/edit")
    @ApiOperation("修改排产状态")
    public AjaxResult edit(@RequestBody Scheduling scheduling){
        List<Scheduling> schedulingList = schedulingService.selectSchedulingById(scheduling.getShiftInfoId(), scheduling.getMachineId());
        int count = 0;
        String workId = "";
        for (Scheduling item : schedulingList) {
            if (item.getCompletedStatus().equals("1")) {
                workId = item.getWorkSheetId();
                count++;
            }
        }
        if (scheduling.getCompletedStatus().equals("1") && count == 1) {
            // 不可以改进行状态
            return AjaxResult.error("当前班次:" + scheduling.getShiftInfoId() + "的设备：" + scheduling.getMachineId() + "目前存在一个正在进行的加工单: " + workId + "无法修改");
        }
        return toAjax(schedulingService.updateCompletedStatus(scheduling));
    }

    /**
     * 删除排产记录
     * @param scheduling
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation("删除排产记录")
    public AjaxResult delete(@RequestBody Scheduling scheduling)
    {

        return toAjax(schedulingService.deleteScheduling(scheduling));
    }

    /**
     * 通过设备编号和班次编号查看排产记录
     * @param shiftInfoId
     * @param machineId
     * @return
     */
    @GetMapping("/searchSchedulingByMachineId")
    @ApiOperation("通过设备编号和班次编号查看排产记录")
    public AjaxResult searchSchedulingByMachineId(@RequestParam String shiftInfoId, @RequestParam String machineId) {
        List<Scheduling> schedulingList = schedulingService.selectSchedulingById(shiftInfoId, machineId);
        if (schedulingList.isEmpty()) {
            return AjaxResult.success("该设备在当前班次并没有排产记录");
        }
        return AjaxResult.success(schedulingList);
    }

    /**
     * 通过加工单编号查询有无排产记录
     * @param workSheetId
     * @return
     */
    @GetMapping("/searchSchedulingByWorkSheetId")
    @ApiOperation("通过加工单编号查询有无排产记录")
    public AjaxResult searchSchedulingByWorkSheetId(@RequestParam String workSheetId) {
        String machineId = schedulingService.searchSchedulingByWorkSheetId(workSheetId);
        if (machineId.equals(" ")) {
            return AjaxResult.success("该加工单当前并未排班");
        }
        return AjaxResult.success(machineId);
    }

    /**
     * 根据加工单编号查询排产记录
     * @param workSheetId
     * @return
     */
    @GetMapping("/selectSchedulingByWorkSheetId")
    @ApiOperation("根据加工单编号查询排产记录")
    public TableDataInfo selectSchedulingByWorkSheetId(@RequestParam String workSheetId) {

        startPage();
        List<Scheduling> schedulingList = schedulingService.selectSchedulingByWorkSheetId(workSheetId);

        return getDataTable(schedulingList);
    }

//    /**
//     * 根据班次查询排产记录
//     *
//     * @param shiftInfoId
//     * @return
//     */
//    @GetMapping("/selectSchedulingByShiftInfoId")
//    @ApiOperation("根据班次查询排产记录")
//    public AjaxResult selectSchedulingByShiftInfoId(@RequestParam String shiftInfoId) {
//        List<Scheduling> schedulingList = schedulingService.selectSchedulingByShiftInfoId(shiftInfoId);
//        return AjaxResult.success(schedulingList);
//    }
}
